import 'package:ati_package/core/routes/app_routes.dart';
import 'package:ati_package/core/utils/ati_enum.dart';
import 'package:ati_package/core/utils/ati_font.dart';
import 'package:carousel_slider/carousel_slider.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

import '../../core/utils/ati_color.dart';

class FindView extends StatefulWidget {
  const FindView({super.key});

  @override
  State<FindView> createState() => _FindViewState();
}

class _FindViewState extends State<FindView> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: buildAppBar(),
      backgroundColor: Color(0xFFF4F8F9),
      body: SingleChildScrollView(
        child: Column(
          children: [
            //顶部按钮条
            buildFunctionRow(),
            //动态
            buildTrendWidget(),
            //新闻资讯
            buildNewsWidget(),
          ],
        ),
      ),
    );
  }

  buildAppBar() {
    return AppBar(
      // backgroundColor: Color(0xFFF4F8F9),
      leadingWidth: 70.w,
      leading: Container(
        margin: EdgeInsets.only(bottom: 7.5.h, top: 7.5.r),
        width: 45.w,
        height: 45.w, // 高度等于宽度，保持正圆
        decoration: BoxDecoration(
          color: Color(0xFFC23A8E),
          shape: BoxShape.circle,
        ),
        child: Center(
          child: Text(
            '浪',
            style: TextStyle(fontSize: 17.sp, color: AtiColor.textHLColor1),
          ),
        ),
      ),
      title: Text(
        '发现',
        style: TextStyle(
          fontSize: AtiFont.font18,
          color: AtiColor.textHLColor1,
        ),
      ),
      actions: [
        Container(
          margin: EdgeInsets.only(bottom: 7.5.h, top: 7.5.r, right: 12.5.r),
          width: 45.w,
          height: 45.w, // 高度等于宽度，保持正圆
          alignment: Alignment.center,
          decoration: BoxDecoration(
            color: Color(0xFFFFFFFF),
            shape: BoxShape.circle,
          ),
          child: IconButton(
            icon: Icon(
              Icons.notifications_none,
              color: AtiColor.primary,
              size: 22.5.r,
            ),
            onPressed: () {
              Get.toNamed(AtiAppRoutes.notice);
            },
          ),
        ),
      ],
      backgroundColor: AtiColor.primary,
    );
  }

  /// 顶部功能区
  Widget buildFunctionRow() {
    final rowImages = [
      {'image': 'find_sign', 'label': '每日签到', 'type': FindModel.findSign},
      {'image': 'find_trend', 'label': '动态', 'type': FindModel.findTrend},
      {'image': 'find_mall', 'label': '积分商城', 'type': FindModel.findMall},
      {'image': 'find_news', 'label': '新闻资讯', 'type': FindModel.findNews},
    ];

    return Container(
      height: 100.r,
      padding: EdgeInsets.symmetric(horizontal: 16.r),
      decoration: BoxDecoration(color: Color(0xFFFFFFFF)),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: rowImages
            .map(
              (item) => InkWell(
                onTap: () {
                  switch (item['type']) {
                    case FindModel.findSign:
                      {
                        Get.toNamed(AtiAppRoutes.sign);
                      }
                      break;
                    case FindModel.findTrend:
                      {
                        Get.toNamed(AtiAppRoutes.trend);
                      }
                    case FindModel.findMall:
                      {
                        Get.toNamed(AtiAppRoutes.pointsMall);
                      }
                    case FindModel.findNews:
                      {
                        Get.toNamed(AtiAppRoutes.news);
                      }
                  }
                  if (item['type'] == FindModel.findSign) {}
                },
                child: Stack(
                  alignment: AlignmentGeometry.center,
                  children: [
                    Container(
                      width: 81.w,
                      height: 81.w,
                      decoration: BoxDecoration(
                        color: Colors.transparent,
                        borderRadius: BorderRadius.circular(16.r),
                      ),
                      child: Image.asset(
                        'assets/images/find/${item['image']}.png',
                        package: 'ati_package',
                      ),
                    ),
                    Positioned(
                      bottom: 12.5.r,
                      child: Text(
                        item['label'] as String,
                        style: TextStyle(
                          fontSize: 12.sp,
                          color: AtiColor.textHLColor1,
                        ),
                      ),
                    ),
                  ],
                ),
              ),
            )
            .toList(),
      ),
    );
  }

  buildHeaderView(String titlStr, String subTitleStr) {
    return Padding(
      padding: EdgeInsets.only(left: 16.r, right: 16.r, top: 16),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Row(
            children: [
              Container(
                width: 4.w,
                height: 14.h,
                decoration: BoxDecoration(
                  color: AtiColor.primary,
                  borderRadius: BorderRadius.circular(2.r),
                ),
              ),
              SizedBox(width: 6.w),
              Text(
                titlStr,
                style: TextStyle(fontSize: 16.sp, fontWeight: FontWeight.w600),
              ),
            ],
          ),
          Spacer(),
          ...[
            Text(
              subTitleStr,
              style: TextStyle(color: AtiColor.textColor1, fontSize: 11.sp),
            ),
            SizedBox(width: 5.r),
            Image.asset(
              width: 16.r,
              height: 16.r,
              'assets/images/common/ati_arrow_forward.png',
              package: 'ati_package',
            ),
          ],
        ],
      ),
    );
  }

  //最新动态
  buildTrendWidget() {
    return Container(
      margin: EdgeInsets.only(top: 10.r),
      decoration: BoxDecoration(color: Color(0xFFFFFFFF)),
      child: Column(
        children: [buildHeaderView('最新动态', '全部动态'), buildTrendListwidget()],
      ),
    );
  }

  buildTrendListwidget() {
    List<int> list = [1, 2, 3, 4, 5];

    return Column(
      children: [
        CarouselSlider(
          options: CarouselOptions(
            height: 260.r,
            viewportFraction: 0.575, // 每张卡片占 80% 屏宽
            enlargeCenterPage: true, // 不放大中间卡片
            padEnds: true, // 左右保留间距
            autoPlay: true,
            enableInfiniteScroll: true, // 不循环滚动
            onPageChanged: (index, reason) {
              setState(() {});
            },
          ),

          items: list.map((item) {
            return Container(
              color: AtiColor.pageColor,
              // color: Colors.red,
              // alignment: Alignment.centerLeft,
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Stack(
                    clipBehavior: Clip.none,
                    children: [
                      Container(
                        margin: EdgeInsets.all(5.r),
                        child: Image.asset(
                          'assets/images/find/trens_list_m.png',
                          package: 'ati_package',
                        ),
                      ),
                      Positioned(
                        top: 147.5.r,
                        left: 15.r,
                        child: Container(
                          width: 38.w,
                          height: 38.w, // 高度等于宽度，保持正圆
                          decoration: BoxDecoration(
                            color: Color(0xFFC23A8E),
                            shape: BoxShape.circle,
                          ),
                          child: Center(
                            child: Text(
                              '浪',
                              style: TextStyle(
                                fontSize: 17.sp,
                                color: AtiColor.textHLColor1,
                              ),
                            ),
                          ),
                        ),
                      ),
                      Positioned(
                        top: 160.r,
                        left: 59.r,
                        child: Text(
                          '浪子',
                          style: TextStyle(
                            fontSize: 14.sp,
                            fontWeight: FontWeight.w600,
                            color: AtiColor.textColor1,
                            height: 2.25,
                          ),
                        ),
                      ),
                    ],
                  ),
                  SizedBox(height: 20.r),
                  Text(
                    '深圳六旬老汉不配合检疫检查还咬伤警察，',
                    style: TextStyle(
                      fontSize: 14.sp,
                      color: AtiColor.textColor1,
                    ),
                  ),
                  Align(
                    alignment: Alignment.centerRight,
                    child: Text(
                      '06/16 08:17',
                      style: TextStyle(
                        fontSize: 12.sp,
                        color: Color(0xFF999999),
                        // height: 2.25,
                      ),
                    ),
                  ),
                ],
              ),
            );
          }).toList(),
        ),

        SizedBox(
          height: 50.r,
          child: Row(
            crossAxisAlignment: CrossAxisAlignment.center,
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Text(
                '全部动态',
                style: TextStyle(color: AtiColor.primary, fontSize: 11.sp),
              ),
              SizedBox(width: 5.r),
              Image.asset(
                width: 16.r,
                height: 16.r,
                color: AtiColor.primary,
                'assets/images/common/ati_arrow_forward.png',
                package: 'ati_package',
              ),
            ],
          ),
        ),
      ],
    );
  }

  buildNewsWidget() {
    return Container(
      margin: EdgeInsets.only(top: 5.r),
      decoration: BoxDecoration(color: Colors.white),
      child: Column(
        children: [buildHeaderView('新闻资讯', '查看更多'), buildNewsListWidget()],
      ),
    );
  }

  buildNewsListWidget() {
    return ListView.builder(
      itemCount: 15,
      shrinkWrap: true, // 关键：嵌套 ListView 时要设置
      physics: const NeverScrollableScrollPhysics(),
      itemBuilder: (context, index) {
        return Container(
          // margin: EdgeInsets.symmetric(vertical: 6.h),
          margin: EdgeInsets.symmetric(vertical: 6.r, horizontal: 15.r),
          padding: EdgeInsets.symmetric(vertical: 10.r, horizontal: 15.r),

          decoration: BoxDecoration(
            color: AtiColor.pageColor,
            borderRadius: BorderRadius.circular(12.r),
          ),
          child: Row(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              // 左边文字
              Expanded(
                child: Container(
                  padding: EdgeInsetsGeometry.symmetric(vertical: 3.r),
                  height: 64.r,
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Text(
                        'ATI 沙棘 NFC 原浆赋能报单计划结束通知',
                        style: TextStyle(
                          fontSize: 14.sp,
                          fontWeight: FontWeight.w600,
                        ),
                        maxLines: 2,
                        overflow: TextOverflow.ellipsis,
                      ),
                      // Spacer(),
                      // SizedBox(height: 16.h),
                      Text(
                        '2025-09-25 00:00:00',
                        style: TextStyle(fontSize: 11.sp, color: Colors.grey),
                      ),
                    ],
                  ),
                ),
              ),
              SizedBox(width: 10.w),
              Image.asset(
                width: 64.r,
                height: 64.r,
                'assets/images/find/news_list_m.png',
                package: 'ati_package',
              ),
            ],
          ),
        );
      },
    );
  }
}
